package com.roger.myHome.controller;

import com.roger.myHome.entity.dto.AssetsUpdateReq;
import com.roger.myHome.entity.qto.AssetsQry;
import com.roger.myHome.page.EUDataGridResult;
import com.roger.myHome.service.AssetsService;
import com.roger.myHome.service.UserService;
import com.roger.myHome.util.UserHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
@RequestMapping("/assets")
@Slf4j
public class AssetsController {
    @Resource
    private UserHelper userHelper;

    @Resource
    private AssetsService assetsService;

    @Resource
    private UserService userService;

    @RequestMapping("/page")
    @ResponseBody
    public EUDataGridResult page(@RequestParam(value="page",required=false) String page,
                                 @RequestParam(value="rows",required=false) String rows, HttpServletRequest req) {

        AssetsQry qryReq = new AssetsQry();
        qryReq.setPage((Integer.parseInt(page) - 1) * Integer.parseInt(rows));
        qryReq.setRows(Integer.parseInt(rows));
        qryReq.setCreateUser(userHelper.getLoginUser(req).getId());
        log.info("[{}]:AssetsController page",userHelper.getLoginUser(req).getLoginName());
        return assetsService.page(qryReq);
    }

    @PutMapping("/update")
    public String update(AssetsUpdateReq req){
        //System.out.println(req);
        return assetsService.update(req);
    }

    @GetMapping("/searchAll")
    public String searchAll(){
        return assetsService.searchAll();
    }

    @GetMapping("/searchAllCash")
    public String searchAllCash(){
        return assetsService.searchAllCash();
    }

    @GetMapping("/buyTicket")
    public String butTicket(){
        ExecutorService executor = Executors.newCachedThreadPool();
        for (int i = 0; i < 100; i++ ) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        userService.doRedisLock();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            });
        }
        return "success:";

    }

}
